package com.wanmait.translate.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author huweibin
 * @Description 日期工具类
 * @Date 12:33 2020/11/23
 **/
public class DateUtil {

    // 转换时间格式（年月日）
    private static String CONVERT_DATE_FORMAT = "yyyy-MM-dd";

    // 转换时间格式（年月日 时分秒 24h制）
    private static String CONVERT_DATE_FORMAT_COMPLETE = "yyyy-MM-dd HH:mm:ss";
    //格式化器
    private static SimpleDateFormat sdf = new SimpleDateFormat(CONVERT_DATE_FORMAT_COMPLETE);
    /**
     * @Author huweibin
     * @Description 截取时间字符串的日期部分
     * @Date 10:39 2020/11/25
     **/
    public static String getDateSubString(String date){
        return date.substring(0, 9);
    }

    /*
     * @Author huweibin
     * @Description 获得指定日期 所在月的最后一天
     * @Date 15:45 2020/11/23
     **/
    public static String getLastDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cal.getTime());
    }

    /**
     * @Author huweibin
     * @Description 获得当前时间的月份
     * @Date 10:35 2020/11/25
     **/
    public static int getNowMonth() {
        List<String> fullDayList = new ArrayList<>();
        return Integer.parseInt(sdf.format(new Date()).substring(5, 7));
    }

    /*
     * @Author huweibin
     * @Description 获得当前时间的字符串
     * @Date 10:34 2020/11/25
     **/
    public static String getNowDate() {
        return sdf.format(new Date());
    }

    /*
     * @Author huweibin
     * @Description 获得指定日期 锁在月的所有天 格式为2020-11-02 00:00:00
     * @Date 15:45 2020/11/23
     **/
    public static List<String> getMonthFullDay(String date) {
        List<String> fullDayList = new ArrayList<>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 0; j <= (count - 1); ) {
            if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
                break;
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(sdf.format(cal.getTime()));
        }
        return fullDayList;
    }

    /**
     * @Author huweibin
     * @Description 获得指定日期 锁在月的所有天 加前后周补齐
     * @Date 15:56 2020/11/23
     **/
    public static List<String> getMonthMoreDay(String date) throws ParseException {
        List<String> monthFullDay = getMonthFullDay(date);
        String start = monthFullDay.get(0);
        String end = monthFullDay.get(monthFullDay.size() - 1);
        Integer startWeek = getWeek(sdf.parse(start));
        Integer endWeek = getWeek(sdf.parse(end));
        //将周日索引改为7
        startWeek = startWeek == 0 ? 7 : startWeek;
        endWeek = endWeek == 0 ? 7 : endWeek;

        List<String> before = new ArrayList<>();
        List<String> after = new ArrayList<>();
        if (startWeek != 1) {
            before = getPastDaysList(start, startWeek);
        }
        if (startWeek == 1 || endWeek == 7) {
            after = getFutureDaysList(end, 15 - endWeek);
        } else {
            after = getFutureDaysList(end, 8 - endWeek);
        }


        //移出重复
        if (before.size() != 0) {
            before.remove(before.size() - 1);
        }
        if (after.size() != 0) {
            after.remove(0);
        }

        //合并
        before.addAll(monthFullDay);
        before.addAll(after);
        return before;
    }

    /**
     * @Author huweibin
     * @Description 获得指定日期是周几
     * @Date 10:35 2020/11/25
     **/
    public static Integer getWeek(Date date) {
        // String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return week_index;
    }

    /**
     * @Author huweibin
     * @Description 获取过去任意天内的日期数组
     * @Date 10:36 2020/11/25
     **/
    public static ArrayList<String> getPastDaysList(String day, int intervals) throws ParseException {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = intervals - 1; i >= 0; i--) {
            pastDaysList.add(getPastDate(day, i));
        }
        return pastDaysList;
    }


    /**
     * @Author huweibin
     * @Description 获取未来任意天内的日期数组
     * @Date 10:36 2020/11/25
     **/
    public static ArrayList<String> getFutureDaysList(String day, int intervals) throws ParseException {
        ArrayList<String> futureDaysList = new ArrayList<>();
        for (int i = 0; i < intervals; i++) {
            futureDaysList.add(getFutureDate(day, i));
        }
        return futureDaysList;
    }


    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return String
     */
    public static String getPastDate(String day, int past) throws ParseException {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(sdf.parse(day));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        String result = sdf.format(calendar.getTime());
        return result;
    }


    /**
     * 获取未来第几天的日期
     *
     * @param past 几天
     * @return String
     */
    public static String getFutureDate(String day, int past) throws ParseException {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(sdf.parse(day));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + past);
        String result = sdf.format(calendar.getTime());
        return result;
    }


}
